//我的页面
const util = require("../../utils/util.js");
import store from "../../utils/factory";
import { logout, getShopCarList} from "../../utils/config";

let that = null;
Component({
  pageLifetimes: {
    show() {
      if (typeof this.getTabBar === "function" && this.getTabBar()) {
        this.getTabBar().setData({
          selected: 3
        });
      }
      if (store.get("userInfo")) {
        this.setData({
          phone: store.get("userInfo").phone
        });
      }
    }
  },
  data: {
    phone: "",
    needPayCount: 0,
    needSendCount: 0,
    needReceivedCount: 0,
    refundCount: 0
  }, // 私有数据，可用于模板渲染
  // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
  attached: function () {
    that = this;
  }, // 此处attached的声明会被lifetimes字段中的声明覆盖
  ready: function () { },
  methods: {
    onShareAppMessage() {
      return {
        title: "天渡大客户基地",
        path: '/pages/index/index'
      }
    },
    logout() {
      wx.showModal({
        content: "确认退出登录?",
        success: function (sm) {
          if (sm.confirm) {
            logout().then(res => {
              store.clear();
              that.setData({
                phone: ""
              });
              that.getTabBar().setData({
                count: 0
              });
            });
          } else if (sm.cancel) {
            console.log("用户点击取消");
          }
        }
      });
    },
    getTotal() {
      getShopCarList().then(res => {
        let req = res.data;
        this.getTabBar().setData({
          count: req.quantity || 0
        });
      });
    },
    goPage(e) {
      if (!store.get("userInfo")) {
        wx.navigateTo({
          url: "../autoLogin/autoLogin" // 页面 B
        });
        return false;
      }
      let url = e.currentTarget.dataset.url;
      wx.navigateTo({
        url: url
      });
    },
    login() {
      wx.navigateTo({
        url: "../autoLogin/autoLogin"
      });
    }
  }
});
